package day20170424.homework;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;

public class HomeWork02 {
	
	/**
	 * 2： 使用字符流 把 io1704下的aaa.txt中的内容复制到bbb.txt  如果是中文会有乱码问题
	 * @throws Exception
	 */
	@Test
	public void homework02(){
		OutputStream os = null;
		InputStream is = null;
		//定义字符输入流
		Reader reader = null;
		//定义字符输出流
		Writer writer = null;
		
		
		try {
			//读入的路径
			File src = new File("txt/aaa.txt");
			//写出的路径
			File dest = new File("txt/bbb.txt");
			
			reader = new FileReader(src);
			writer = new FileWriter(dest);
			
			char[] ch = new char[3];
			int len;
			while((len = reader.read(ch)) != -1){
				writer.write(ch, 0, len);
				writer.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(null != reader){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != writer){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != os){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
